package com.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.dto.AnswerDTO;
import com.demo.dto.Result;
import com.demo.dto.UserDTO;
import com.demo.entity.Answer;
import com.demo.mapper.AnswerMapper;
import com.demo.service.AnswerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.utils.SystemConstants;
import com.demo.utils.UserHolder;
import com.demo.vo.AnswerVO;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Zeng
 * @since 2022-03-23
 */
@Service
public class AnswerServiceImpl extends ServiceImpl<AnswerMapper, Answer> implements AnswerService {

    @Override
    public Result<Answer> postAnswer(AnswerDTO answerDTO) {
        Answer answer = BeanUtil.copyProperties(answerDTO, Answer.class);
        Integer userId = UserHolder.getUser().getUserId();
        answer.setUserId(userId);
        if (!save(answer)) {
            return Result.fail("回答新增失败");
        }
        return Result.ok(getById(answer.getAnswerId()), "回答新增成功");
    }

    // 查看是否有权限删改回答，如果是该用户新建的回答则返回Answer对象，否则为null
    Answer getAnswerByIdAndUserId(Integer answerId) {
        Integer userId = UserHolder.getUser().getUserId();
        QueryWrapper<Answer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("answer_id", answerId);
        return getOne(queryWrapper);
    }

    @Override
    public Result<Answer> deleteAnswer(Integer answerId) {
        Answer answer = getAnswerByIdAndUserId(answerId);
        if (answer == null) {
            return Result.fail("answer不存在或没有删除权限");
        }
        // 删除回答
        removeById(answerId);
        // 返回被删除的回答
        return Result.ok(answer, "回答删除成功");
    }

    @Override
    public Result<Answer> updateAnswer(Integer answerId, AnswerDTO answerDTO) {
        Answer answer = getAnswerByIdAndUserId(answerId);
        if (answer == null) {
            return Result.fail("answer不存在或没有更新权限");
        }
        // 更新问题
        Answer answerToUpdate = BeanUtil.copyProperties(answerDTO, Answer.class);
        answerToUpdate.setAnswerId(answerId);

        if (!updateById(answerToUpdate)) {
            return Result.fail("回答更新失败");
        }
        return Result.ok(getById(answerId));
    }

    List<AnswerVO> getAnswers(String questionId, Integer userId, Integer currentPage, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("questionId", questionId);
        map.put("userId", userId);
        map.put("start", (currentPage - 1) * pageSize);
        map.put("offset", pageSize);
        return baseMapper.queryAnswersByQuestionId(map);
    }

    @Override
    public Result<List<AnswerVO>> queryAnswersByQuestionId(String questionId, Integer current) {
        UserDTO user = UserHolder.getUser();
        Integer userId = user == null ? null : user.getUserId();

        List<AnswerVO> answers = getAnswers(questionId, userId, current, SystemConstants.MAX_PAGE_SIZE);
        if (answers.isEmpty()) {
            return Result.fail("当前answer内容为空");
        }
        return Result.ok(answers, answers.size());
    }

    @Override
    public Result<List<AnswerVO>> getAnswerListByCurrentUserId() {
        Integer userId = UserHolder.getUser().getUserId();
        return queryAnswersByUserId(userId);
    }

    @Override
    public Result<List<AnswerVO>> queryAnswersByUserId(Integer userId) {
        List<AnswerVO> answerVOS = baseMapper.queryAnswersByUserId(userId);
        if (answerVOS.isEmpty()) {
            return Result.fail("该用户没有回答");
        }
        return Result.ok(answerVOS, answerVOS.size());
    }
}
